package com.dmcloud.dm.auth.api;

import com.dmcloud.common.constant.SecurityConstants;
import com.dmcloud.common.util.reponse.ResultMessage;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api")
public class TestController {

    @GetMapping("/test")
    public ResultMessage test(){
        return ResultMessage.success("测试成功");
    }
    @GetMapping("/test2")
    public ResultMessage test2(HttpServletRequest request){
        System.out.println(request);
        return ResultMessage.success("测试成功");
    }

    @GetMapping("/test3")
    @SneakyThrows
    public ResultMessage test3(HttpServletRequest request){
        String header = request.getHeader("Authorization");
        String token = StringUtils.substringAfter(header, "bearer ");
        System.out.println(token);
        Claims body = Jwts.parser().setSigningKey(SecurityConstants.JWT_SIGN_KEY.getBytes("UTF-8")).parseClaimsJws(token).getBody();
        System.out.println(body.toString());
        return ResultMessage.success("测试成功");
    }
}
